package com.gexiaobao.pigeon.ui.view

/**
 * @Author : hxw
 * @Date : 2024/7/4 11:08
 * @Describe :
 */
import android.content.Context
import android.graphics.Canvas
import android.graphics.drawable.Drawable
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.gexiaobao.pigeon.R

class DividerItemDecoration(context: Context, orientation: Int) : RecyclerView.ItemDecoration() {

    private var divider: Drawable? = ContextCompat.getDrawable(context, R.drawable.custom_divider) // 自定义分割线的 Drawable

    override fun onDraw(canvas: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        val left = parent.paddingLeft
        val right = parent.width - parent.paddingRight

        val childCount = parent.childCount
        for (i in 0 until childCount - 1) {
            val child = parent.getChildAt(i)

            val params = child.layoutParams as RecyclerView.LayoutParams
            val top = child.bottom + params.bottomMargin
            val bottom = top + divider!!.intrinsicHeight

            divider!!.setBounds(left, top, right, bottom)
            divider!!.draw(canvas)
        }
    }
}

